package aar.test.tessocr;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

import aar.test.tessocr.baseApplication.BaseApplication;
import aar.test.tessocr.model.DialogCallback;
import aar.test.tessocr.model.GridDetailModel;
import aar.test.tessocr.model.LzyResponse;
import aar.test.tessocr.model.loginModel;
import aar.test.tessocr.utils.LogUtils;
import aar.test.tessocr.utils.Urls;
import clx.tessocr.ocreng.R;
import okgo.OkGo;
import okgo.model.Response;
import okhttp3.RequestBody;

import static aar.test.tessocr.LoginActivity.JSON;

public class CabInfoActivity extends AppCompatActivity {

    private TextView grid_num,title_company_name,title_company_contact,title_company_phone,title_save_num;

    private LinearLayout left_layout;

    private Button grid_open;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cab_info);
        initView();


        companyGridDetail();

    }

    private void initView() {

        grid_num = findViewById(R.id.grid_num);
        title_company_name = findViewById(R.id.title_company_name);
        title_company_contact = findViewById(R.id.title_company_contact);
        title_company_phone = findViewById(R.id.title_company_phone);
        title_save_num = findViewById(R.id.title_save_num);

        grid_open = findViewById(R.id.grid_open);
        grid_open.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                adminOpenOneGrid();
            }
        });




        left_layout=findViewById(R.id.left_layout);
        left_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }






    GridDetailModel mGridDetailModel=null;
    private void companyGridDetail(){

        if(null==BaseApplication.getIntance().getmCabDetailModel()){
            return;
        }

        Map<String ,Object> map=new HashMap<>();
        map.put("cabinetGridId",BaseApplication.getIntance().getmCabDetailModel().getCabinetgridid());

        Gson gg=new Gson();
        String json =gg.toJson(map);

        RequestBody body = RequestBody.create(JSON,json);
        OkGo.<LzyResponse<GridDetailModel>>post(Urls.URL_GRIDDETAIL)//
                .tag(this)//
                .upRequestBody(body)
                .isMultipart(false)         //强制使用 multipart/form-data 表单上传（只是演示，不需要的话不要设置。默认就是false）
                .execute(new DialogCallback<LzyResponse<GridDetailModel>>(CabInfoActivity.this) {
                    @Override
                    public void onSuccess(Response<LzyResponse<GridDetailModel>> response) {

                        mGridDetailModel=response.body().data;
                        Log.e("MainActivity","onSuccess-----logout---------------"+response.body().data);

                        if(null!=mGridDetailModel){

                             runOnUiThread(new Runnable() {
                                 @Override
                                 public void run() {


                                     if(!TextUtils.isEmpty(mGridDetailModel.getCabinetgridid())){
                                         LogUtils.e("1111111111111111111111111"+mGridDetailModel.getCabinetgridid());
                                         grid_num.setText("格子编号："+mGridDetailModel.getGridnum());
                                     }else {

                                         LogUtils.e("2222222222222");
                                         grid_num.setText("暂无信息，请添加");
                                     }

                                     if(null!=mGridDetailModel.getCompanyname()){
                                         LogUtils.e("33333333333333=="+mGridDetailModel.getCompanyname());
                                         title_company_name.setText("公司名称："+mGridDetailModel.getCompanyname());
                                     }else {
                                         LogUtils.e("4444444444444");
                                         title_company_name.setText("暂无信息，请添加");
                                     }


                                     if(null!=mGridDetailModel.getContactname()){
                                         title_company_contact.setText("公司联系人："+mGridDetailModel.getContactname());
                                     }else {
                                         title_company_contact.setText("暂无信息，请添加");
                                     }

                                     if(null!=mGridDetailModel.getContact()){
                                         title_company_phone.setText("公司电话："+mGridDetailModel.getContact());
                                     }else {
                                         title_company_phone.setText("暂无信息，请添加");
                                     }

                                     if(null!=mGridDetailModel.getGridnum()){

                                         title_save_num.setText("存放数："+mGridDetailModel.getSku());
                                     }  else {
                                         title_save_num.setText("暂无信息，请添加");
                                     }








                                 }
                             });


                        }



                    }

                    @Override
                    public void onError(Response<LzyResponse<GridDetailModel>> response) {
                        Log.e("MainActivity","onError---------------------");
                    }
                });



    }






    private void adminOpenOneGrid(){

        if(null==BaseApplication.getIntance().getmCabDetailModel()){
            return;
        }

        if(null==BaseApplication.getIntance().getmCabDetailModel().getCabinetgridid()){
            return;
        }

        Map<String ,Object> map=new HashMap<>();
        map.put("cabinetId",BaseApplication.getIntance().getmCabDetailModel().getCabinetid());
        map.put("type",2);
        map.put("gridNum",BaseApplication.getIntance().getmCabDetailModel().getGridnum());

        Gson gg=new Gson();
        String json =gg.toJson(map);

        RequestBody body = RequestBody.create(JSON,json);
        OkGo.<LzyResponse>post(Urls.URL_ADMINOPENGRID)//
                .tag(this)//
                .upRequestBody(body)
                .isMultipart(false)         //强制使用 multipart/form-data 表单上传（只是演示，不需要的话不要设置。默认就是false）
                .execute(new DialogCallback<LzyResponse>(CabInfoActivity.this) {
                    @Override
                    public void onSuccess(Response<LzyResponse> response) {


                        Log.e("MainActivity","onSuccess-----logout---------------"+response.body().data);


                        if(null!=response.body().msg){
                            Toast.makeText(CabInfoActivity.this,response.body().msg,Toast.LENGTH_LONG).show();
                        }


                    }

                    @Override
                    public void onError(Response<LzyResponse> response) {
                        Log.e("MainActivity","onError---------------------");
                        if(null!=response.body().msg){
                            Toast.makeText(CabInfoActivity.this,response.body().msg,Toast.LENGTH_LONG).show();}
                    }
                });



    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        //Activity销毁时，取消网络请求
        OkGo.getInstance().cancelTag(this);




    }



}
